#!/bin/sh
# https://store.docker.com/images/postgres 

# config
CONF_FILE=/Users/chris/dev/docker_test/dev_dockfile/redis/redis.conf
VMOUNT="-v /data-scm/mnt/db-master/:/var/lib/postgresql/data"
E_PASSWORD="-e POSTGRES_PASSWORD=mysecretpassword"

#E_PGDATA="-e PGDATA"  # default /var/lib/postgresql/data

IMG=postgres:csis
CNAME=csis-postgres
BIND_PORT="-p 5432:5432"  
IMG_RUN_CMD=""
#docker run -it --rm --link some-postgres:postgres postgres psql -h postgres -U postgres

if [ $# -lt 1 ]; then
    echo "error, run ./service.sh 

 (start|stop|rm)"
    exit 1
fi

if [ $1 = "start" ]; then
	#  check & mount the NAS or SAN driver
  

	# @todo: mount !
	# -o option 
	# rw read/write
  #mount -o rw /dev/sdb1 /data-scm/mnt/db-master

  existContainerId=`docker ps |grep $CNAME|awk '{print $1 }'`
  if [ ! $existContainerId = "" ]; then
    echo "The container is running (cid: $existContainerId )"
    docker ps |grep $CNAME
    exit 1
  fi
  echo "Starting $CNAME images"
  CMD="docker run -d  --rm $VMOUNT $BIND_PORT --name $CNAME $IMG $IMG_RUN_CMD"  
  #CMD="docker run -d  --rm $BIND_PORT --name $CNAME $IMG $IMG_RUN_CMD"

  echo $CMD
  $CMD
  docker ps |grep $CNAME |awk '{print "The docker container ID is :" $1 }'
  echo "Start success! You can attach the docker with command : \"docker attach {cid}\" or \"docker exec -it {cid} bash\""  
fi

if [ $1 = "stop" ]; then
  echo "Stoping $CNAME images"
  docker ps |grep $CNAME |awk '{print "docker stop " $1 }'|sh
  echo "docker ps "
  docker ps |grep $CNAME

  # umount !!!!
  #umount /data-scm/mnt/db-master/
fi
 

if [ $1 = "login" ]; then

  docker exec -it $CNAME /bin/bash

fi
